package carassius.GUI;

import java.awt.Color;
import java.awt.Component;
import java.util.IdentityHashMap;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author siebz0r
 */
public class WaterValuesTable
		extends JTable
{
	private Map<Integer, Boolean> _rowValid;

	public WaterValuesTable()
	{
		super(new DefaultTableModel(0, 0));
		putClientProperty("terminateEditOnFocusLost", true);
		_rowValid = new IdentityHashMap<Integer, Boolean>();

		DefaultTableModel model = (DefaultTableModel) getModel();
		model.addColumn("WaterValue");
		model.addColumn("MinValue");
		model.addColumn("MaxValue");

		TableColumn minimalValueColumn = getColumn(getColumnName(1));
		DoubleTableCellRenderer doubleTableCellRenderer = new DoubleTableCellRenderer();
		DoubleTableCellEditor doubleTableCellEditor = new DoubleTableCellEditor();

		minimalValueColumn.setCellRenderer(doubleTableCellRenderer);
		minimalValueColumn.setCellEditor(doubleTableCellEditor);

		TableColumn maximalValueColumn = getColumn(getColumnName(2));
		maximalValueColumn.setCellRenderer(doubleTableCellRenderer);
		maximalValueColumn.setCellEditor(doubleTableCellEditor);
	}

	@Override
	public void tableChanged(TableModelEvent e)
	{
		super.tableChanged(e);
		if (e.getFirstRow() >= 0)
		{
			switch (e.getType())
			{
				case TableModelEvent.INSERT:
				case TableModelEvent.UPDATE:
					for (int i = e.getFirstRow(); i <= e.getLastRow(); i++)
					{
						_rowValid.put(i, validateRow(i));
					}
					break;
				case TableModelEvent.ALL_COLUMNS:
					validateAllRows();
					break;
			}
		}
	}

	@Override
	public Component prepareRenderer(TableCellRenderer renderer, int row,
									 int column)
	{
		if (column != 0)
		{
			JComponent prepareRenderer = (JComponent) super.prepareRenderer(renderer, row, column);
			if (!validateRow(row))
			{
				prepareRenderer.setBorder(new LineBorder(Color.RED, 1));
			}
			else
			{
				prepareRenderer.setBorder(null);
			}
			return prepareRenderer;
		}
		else
		{
			return super.prepareRenderer(renderer, row, column);
		}
	}

	public boolean isInputValid()
	{
		return !_rowValid.containsValue(false);
	}

	private boolean validateRow(int row)
	{
		if (((DoubleTableCellRenderer) getCellRenderer(row, 1)).isValueValid()
				&& ((DoubleTableCellRenderer) getCellRenderer(row, 2)).isValueValid())
		{
			Double min = (Double) getValueAt(row, 1);
			Double max = (Double) getValueAt(row, 2);
			if (min == null || max == null)
			{
				return true;
			}
			if (min <= max)
			{
				return true;
			}
		}
		return false;
	}

	private boolean validateAllRows()
	{
		_rowValid.clear();
		DefaultTableModel model = (DefaultTableModel) getModel();
		for (int i = 0; i < model.getRowCount(); i++)
		{
			_rowValid.put(i, validateRow(i));
		}
		return !_rowValid.containsValue(false);
	}
}
